package me.zhyx.controller;

import com.alibaba.fastjson.JSON;
import me.zhyx.entity.User;
import me.zhyx.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;
    @RequestMapping(value = "/getUserById",method = RequestMethod.GET)
    @ResponseBody
    public String getUserById(@RequestParam Integer userId, HttpServletRequest request, HttpServletResponse response) {
        //调用service方法得到用户列表
        logger.info("根据用户id查询用户信息{}",userId);
        return JSON.toJSONString(userService.getUserById(userId));
    }
    @RequestMapping(value = "/insertUser",method = RequestMethod.GET)
    @ResponseBody
    public Integer insertUser(@ModelAttribute User user, HttpServletRequest request, HttpServletResponse response) {
        //调用service方法得到用户列表
        logger.info("插入用户",user);
        return userService.insertUser(user);
    }
}
